﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace coliseu.BussinessLayer
{
    public class MoradorBL
    {
        private coliseuEntities cEntities;

        public MoradorBL()
        {
            cEntities = new coliseuEntities(Properties.Settings.Default.conn_coliseu);
        }

        public List<morador> selecionar()
        {
            return (from x in cEntities.morador select x).ToList();
        }

        public List<morador> selecionar(string nome)
        {
            return (from x in cEntities.morador where x.nome.Contains(nome) select x).ToList();
        }

        public void salvar(morador b, string tipo)
        {

            if (tipo == "insert")
            {
                cEntities.AddTomorador(b);
            }
            else
            {
                cEntities.morador.ApplyCurrentValues(b);
            }
            cEntities.SaveChanges();
        }

        public void excluir(morador b)
        {            
            cEntities.morador.DeleteObject(b);

            List<morador_apartamento> x = cEntities.morador_apartamento.Where(u => u.id_morador == b.id_morador).ToList();
            foreach (morador_apartamento f in x)
            {

                cEntities.morador_apartamento.DeleteObject(f);
            }

            List<morador_contato> y = cEntities.morador_contato.Where(u => u.id_morador == b.id_morador).ToList();
            foreach (morador_contato f in y)
            {

                cEntities.morador_contato.DeleteObject(f);
            }

            cEntities.SaveChanges();
        }
    }
}
